<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域视图 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input clearable v-model="queryInfo.query" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="searchData"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"><el-button type="primary" @click="addGoods">添加商品</el-button></el-col>
      </el-row>
      <!-- 列表区域 -->
      <el-table
        :data="goodsTableData" border  style="width: 100%">
        <el-table-column  label="#" width="180" type="index"> </el-table-column>
        <el-table-column  prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column  prop="goods_price" label="商品价格（元）"> </el-table-column>
        <el-table-column  prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column  prop="add_time" label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time | dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary"  icon="el-icon-edit" @click="editGoodsInfo(scope.row.goods_id)" ></el-button>
            <el-button  size="mini"  type="danger"  icon="el-icon-delete" @click="deleteGoodsInfo(scope.row.goods_id)" ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 6, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 修改商品对话框 -->
      <el-dialog
          title="修改商品"
          :visible.sync="editGoodsDialogVisible"
          width="50%" @close="editDialogClosed">
          <el-form ref="editGoodsFormRef" :model="eidtGoodsForm" label-width="100px" :rules="eidtGoodsFormRules">
            <el-form-item label="商品名称"  prop="goods_name">
              <el-input v-model="eidtGoodsForm.goods_name" ></el-input>
            </el-form-item>
            <el-form-item label="商品价格（元）"  prop="goods_price">
              <el-input v-model="eidtGoodsForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量"  prop="goods_weight">
              <el-input v-model="eidtGoodsForm.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量"  prop="goods_number">
              <el-input v-model="eidtGoodsForm.goods_number"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitEditGoods">确 定</el-button>
          </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品列表数据
      goodsTableData: [],
      // 得到商品列表的
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 3
      },
      total: 0,
      // 修改用户对话框
      editGoodsDialogVisible: false,
      // 修改用户对话框form
      eidtGoodsForm: {},
      // 修改用户表单的验证规则
      eidtGoodsFormRules: {
        goods_name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getGoodsData()
  },
  methods: {
    // 获取用户数据
    async getGoodsData () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      // console.log(res)
      this.goodsTableData = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsData()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsData()
    },
    // 实现搜索功能
    searchData () {
      this.getGoodsData()
    },
    // 删除商品
    deleteGoodsInfo (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)
        // console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        } else {
          this.$message.success('删除成功')
          this.getGoodsData()
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 编辑商品信息
    async editGoodsInfo (id) {
      const { data: res } = await this.$http.get(`goods/${id}`)
      console.log(res)
      if (res.meta.status === 200) {
        this.eidtGoodsForm = res.data
        this.editGoodsDialogVisible = true
      } else {
        this.$message.error('查询失败')
      }
    },
    // 修改用户对话框关闭进行的操作
    editDialogClosed () {
      this.$refs.editGoodsFormRef.resetFields()
    },
    // 编辑用户对话框提交
    submitEditGoods () {
      this.$refs.editGoodsFormRef.validate(async valid => {
        if (valid) {
          console.log(this.eidtGoodsForm)
          const { data: res } = await this.$http.put(`goods/${this.eidtGoodsForm.goods_id}`, {
            goods_name: this.eidtGoodsForm.goods_name,
            goods_price: this.eidtGoodsForm.goods_price,
            goods_number: this.eidtGoodsForm.goods_number,
            goods_weight: this.eidtGoodsForm.goods_weight,
            goods_cat: this.eidtGoodsForm.goods_cat
          })
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message.success('修改成功')
            this.editGoodsDialogVisible = false
            this.getGoodsData()
          } else {
            this.$message.error(res.meta.msg)
          }
        } else {
          return false
        }
      })
    },
    // 添加商品对话框
    addGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>
.el-row{
    margin-bottom: 15px
}
</style>
